package com.todo.biz.application.convert;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.todo.biz.application.entity.bo.BaseDictBO;
import com.todo.biz.iface.backend.request.InsertDictReq;
import com.todo.biz.iface.backend.request.UpdateDictReq;
import com.todo.biz.iface.backend.response.DictDetailResp;
import com.todo.biz.iface.frontend.response.DictDetailVO;
import com.todo.biz.infrastructure.entity.po.BaseDictPO;
import com.todo.biz.infrastructure.entity.po.DictValue;
import com.todo.common.utils.TodoCommonUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2024/12/9 15:48
 * @description
 */
public class DictConvert {

    public static BaseDictPO convertToBaseDictPO(BaseDictBO baseDictBO) {
        if (baseDictBO == null) {
            return null;
        }
        BaseDictPO baseDictPO = new BaseDictPO();
        baseDictPO.setId(baseDictBO.getId() == null ? TodoCommonUtils.buildLongGuid() : baseDictBO.getId());
        baseDictPO.setBizCode(baseDictBO.getBizCode());
        baseDictPO.setSceneCode(baseDictBO.getSceneCode());
        baseDictPO.setValues(JSONObject.toJSONString(baseDictBO.getDictValues()));
        baseDictPO.setIsDelete(baseDictBO.getIsDelete() == null ? 0 : baseDictBO.getIsDelete());
        baseDictPO.setCreateTime(baseDictBO.getCreateTime() == null ? new Date() : baseDictBO.getCreateTime());
        baseDictPO.setUpdateTime(baseDictBO.getUpdateTime() == null ? new Date() : baseDictBO.getUpdateTime());
        return baseDictPO;
    }

    public static BaseDictBO convertToBaseDictBO(BaseDictPO baseDictPO) {
        if (baseDictPO == null) {
            return null;
        }
        BaseDictBO baseDictBO = new BaseDictBO();
        baseDictBO.setId(baseDictPO.getId());
        baseDictBO.setBizCode(baseDictPO.getBizCode());
        baseDictBO.setSceneCode(baseDictPO.getSceneCode());
        baseDictBO.setDictValues(JSONObject.parseArray(baseDictPO.getValues(), DictValue.class));
        baseDictBO.setIsDelete(baseDictPO.getIsDelete());
        baseDictBO.setCreateTime(baseDictPO.getCreateTime());
        baseDictBO.setUpdateTime(baseDictPO.getUpdateTime());
        return baseDictBO;
    }

    public static BaseDictBO buildToBaseDictBO(InsertDictReq req) {
        if (req == null) {
            return null;
        }
        BaseDictBO baseDictBO = new BaseDictBO();
        baseDictBO.setId(TodoCommonUtils.buildLongGuid());
        baseDictBO.setBizCode(req.getBizCode());
        baseDictBO.setSceneCode(req.getSceneCode());
        baseDictBO.setDictValues(convertToBODictValue(req.getValues()));
        baseDictBO.setIsDelete(0);
        baseDictBO.setCreateTime(new Date());
        baseDictBO.setUpdateTime(new Date());
        return baseDictBO;
    }

    public static BaseDictBO buildToBaseDictBO(UpdateDictReq req) {
        if (req == null) {
            return null;
        }
        BaseDictBO baseDictBO = new BaseDictBO();
        baseDictBO.setId(req.getId());
        baseDictBO.setBizCode(req.getBizCode());
        baseDictBO.setSceneCode(req.getSceneCode());
        baseDictBO.setDictValues(convertToBODictValue(req.getValues()));
        baseDictBO.setIsDelete(0);
        baseDictBO.setCreateTime(new Date());
        baseDictBO.setUpdateTime(new Date());
        return baseDictBO;
    }

    private static List<DictValue> convertToBODictValue(List<com.todo.biz.iface.backend.request.DictValue> values) {
        return JSONObject.parseArray(JSONObject.toJSONString(values), DictValue.class);
    }

    public static DictDetailResp convertToDictDetailResp(BaseDictBO baseDictBO) {
        if (baseDictBO == null) {
            return null;
        }
        DictDetailResp resp = new DictDetailResp();
        resp.setId(baseDictBO.getId());
        resp.setBizCode(baseDictBO.getBizCode());
        resp.setSceneCode(baseDictBO.getSceneCode());
        resp.setValues(convertToIfaceDictValue(baseDictBO.getDictValues()));
        return resp;
    }

    private static List<com.todo.biz.iface.backend.request.DictValue> convertToIfaceDictValue(List<DictValue> dictValues) {
        return JSONObject.parseArray(JSONObject.toJSONString(dictValues), com.todo.biz.iface.backend.request.DictValue.class);
    }

    public static List<DictDetailResp> convertToDictDetailRespList(List<BaseDictBO> baseDictBOS) {
        if (CollectionUtils.isEmpty(baseDictBOS)) {
            return Lists.newArrayList();
        }
        return baseDictBOS.stream().map(DictConvert::convertToDictDetailResp).collect(java.util.stream.Collectors.toList());
    }

    public static DictDetailVO convertToDictDetailVO(BaseDictBO baseDictBO) {
        if (baseDictBO == null) {
            return null;
        }
        DictDetailVO vo = new DictDetailVO();
        vo.setValues(convertToVODictValues(baseDictBO.getDictValues()));
        vo.setSceneCode(baseDictBO.getSceneCode());
        vo.setBizCode(baseDictBO.getBizCode());
        vo.setId(baseDictBO.getId());
        return vo;
    }

    private static List<com.todo.biz.iface.frontend.response.DictValue> convertToVODictValues(List<com.todo.biz.infrastructure.entity.po.DictValue> dictValues) {
        if (CollectionUtils.isEmpty(dictValues)) {
            return Lists.newArrayList();
        }
        return JSONObject.parseArray(JSONObject.toJSONString(dictValues), com.todo.biz.iface.frontend.response.DictValue.class);
    }
}
